package com.tenbit.tbw.svc;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tenbit.tbw.dao.BonusEmployeeListEDAO;
import com.tenbit.tbw.vo.BON1010VO;

/**
 * 상품 정보목록 조회 담당SVC <br>
 * <PRE>
 * System Name : 텐비트 워크샵
 * Business Name : 보너스(BON)
 * Author : 김창섭
 * Class Name(en) : BON1010SVCImpl.java
 * Class Name(ko) : 보너스관리 SVCImpl
 * Description  : 보너스 목록 조회, 정보 입력,수정,삭제 데이터 검사 DAO호출
 * Modification History
 *     수정일         수정자     수정내용
 *    ------------  -------  ---------------------------
 *    2014.04.23.   김창섭   	 최초 생성
 * </PRE>
 */

@Service
public class BON1010SVCImpl implements BON1010SVC {
	private Logger log=Logger.getLogger(this.getClass());
	
	@Autowired
	private BonusEmployeeListEDAO dao;
	
	private Map<String,Object> resultMap=new HashMap<String,Object>();
	private Calendar calendar = Calendar.getInstance( );
	boolean result=false;
	private String msg=null;
	
	
	@Override
	public  Map<String, Object> getEmployeeInfoList(Map<String, Object> paramMap){
		
		log.debug("==========getEmployeeInfoList:시작==========");
		String departmentCode=(String) paramMap.get("departmentCode");
		
		if(departmentCode==null ||departmentCode.equals("")){
			departmentCode="BUSINESS";
			paramMap.put("departmentCode",departmentCode);
		} else if(departmentCode.equals("all")){
			departmentCode="";
			paramMap.put("departmentCode",departmentCode);
		}
		
		try {
			
			List<BON1010VO> rows = dao.listEmployeeInfo(paramMap);
			resultMap.put("rows",rows);
			log.debug(rows);
			
		} catch (Exception e) {
			log.debug(e);
		}
		return resultMap;
	}
	
	@Override
	public  Map<String, Object> getEmployeeBonusInfoList(Map<String, Object> paramMap){
		
		log.debug("==========getEmployeeBonusInfoList:시작==========");

		
		String departmentCode=(String) paramMap.get("departmentCode");
		String bonusCode=(String) paramMap.get("bonusCode");
		String bonusDate1= (String) paramMap.get("bonusDate1");
		String bonusDate2=(String) paramMap.get("bonusDate2");
		
		
		if((bonusDate1==null ||bonusDate1.equals("")) && (bonusDate2==null ||bonusDate2.equals(""))){
			bonusDate1=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+1;
			bonusDate2=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH);
			
		} else if((bonusDate1!=null) && (bonusDate2==null ||bonusDate2.equals(""))){
			bonusDate2= 9999+"-"+"12"+"-"+"31";
			
		} else if((bonusDate1==null ||bonusDate1.equals("")) && (bonusDate2!=null)){
			bonusDate1= 1900+"-"+"01"+"-"+"01";
			
		} else if(((departmentCode!=null && !departmentCode.equals("")) ||(bonusCode!=null && !bonusCode.equals("")))
				&&(bonusDate1==null ||bonusDate1.equals("")) && (bonusDate2==null ||bonusDate2.equals(""))){
			bonusDate1= 1900+"-"+"01"+"-"+"01";
			bonusDate2= 9999+"-"+"12"+"-"+"31";
		} else if(bonusDate1!=null && bonusDate2!=null){
			String[] start1=bonusDate1.split("-");
			String[] start2=bonusDate2.split("-");
			
			int startYear1=Integer.parseInt(start1[0]);
			int startMonth1=Integer.parseInt(start1[1]);
			int startDay1=Integer.parseInt(start1[2]);
			
			int startYear2=Integer.parseInt(start2[0]);
			int startMonth2=Integer.parseInt(start2[1]);
			int startDay2=Integer.parseInt(start2[2]);

			if(startYear1>startYear2
					||(startYear1==startYear2 && startMonth1>startMonth2)
					||(startYear1==startYear2 && startMonth1==startMonth2 && startDay1>startDay2))
			{
				
				msg="처리 실패 : bonusDate1 가 bonusDate2 보다 큽니다 ";
				result=false;
				resultMap.put("result",result);
				resultMap.put("msg",msg);
				log.debug(resultMap);
				return resultMap;
			}
		}
		
		paramMap.put("bonusDate1",bonusDate1);
		paramMap.put("bonusDate2",bonusDate2);
		
		if(departmentCode==null ||departmentCode.equals("")){
			departmentCode="BUSINESS";
			paramMap.put("departmentCode",departmentCode);
		} else if(departmentCode.equals("all")){
			departmentCode="";
			paramMap.put("departmentCode",departmentCode);
		}
		paramMap.put("departmentCode",departmentCode);
		if(bonusCode==null ||bonusCode.equals("")){
			bonusCode="B1";
			paramMap.put("bonusCode",bonusCode);
		} else if(bonusCode.equals("all")){
			bonusCode="";
			paramMap.put("bonusCode",bonusCode);
		}
		paramMap.put("bonusCode",bonusCode);
		try {
			
			List<BON1010VO> rows = dao.listBonusInfo(paramMap);
			resultMap.put("rows",rows);
			log.debug(rows);
			
		} catch (Exception e) {
			log.debug(e);
		}
		 
		msg="처리성공 : 정상적으로 처리되었습니다.";
		result=true;
		resultMap.put("result",result);
		resultMap.put("msg",msg);
		log.debug("==========getEmployeeBonusInfoList:종료==========");
		log.debug(resultMap);
		
		return resultMap;
	}
	
	@Override
	public  Map<String, Object> setEmployeeBonusInfoList(List<Map<String,Object> > vo){
		log.debug("==========setEmployeeBonusInfoList:시작==========");
		Map<String, Object> param = new HashMap<String, Object>();
				
		for(int i=0;i<vo.size();i++){
	
			if(vo.get(i).get("bonusDate")==null||vo.get(i).get("bonusDate").equals("")){
				msg="처리실패 : 지급일자가 없습니다. ";
				result=false;
				resultMap.put("result",result);
				resultMap.put("msg",msg);
				return resultMap;
			} else if(vo.get(i).get("bonusCode")==null||vo.get(i).get("bonusCode").equals("")){
				msg="처리실패 : 보너스 종류가 없습니다. ";
				result=false;
				resultMap.put("result",result);
				resultMap.put("msg",msg);
				return resultMap;
			} else if(vo.get(i).get("bonusSalary")==null||vo.get(i).get("bonusSalary").equals("")){
				msg="처리실패 : 지급 금액이 없습니다.";
				result=false;
				resultMap.put("result",result);
				resultMap.put("msg",msg);
				return resultMap;
			} else if(vo.get(i).get("bonusSalary").toString().length()<4){
				msg="처리실패 : 지급 금액이 작습니다.";
				result=false;
				resultMap.put("result",result);
				resultMap.put("msg",msg);
				return resultMap;
			} else if(vo.get(i).get("sign").equals("I")){
				
				param.put("employeeCode",vo.get(i).get("employeeCode"));
				param.put("bonusCode",vo.get(i).get("bonusCode"));
				param.put("bonusDate",vo.get(i).get("bonusDate"));
				param.put("bonusSalary",vo.get(i).get("bonusSalary"));
				
				try {
					dao.insEmployeeBonusInfo(param);
				} catch (Exception e) {
					msg="처리실패 : Insert 예외 발생, 중복값 확인 바랍니다";
					result=false;
					resultMap.put("result",result);
					resultMap.put("msg",msg);
					return resultMap;
				}//try
				
							
			} else if(vo.get(i).get("sign").equals("U")){
				
				param.put("employeeCode",vo.get(i).get("employeeCode"));
				param.put("bonusCode",vo.get(i).get("bonusCode"));
				param.put("bonusDate",vo.get(i).get("bonusDate"));
				param.put("bonusSalary",vo.get(i).get("bonusSalary"));
				try {
					dao.setEmployeeBonusInfo(param);
				} catch (Exception e) {
					msg="처리실패 : Update 예외 발생, 중복값 확인 바랍니다.";
					result=false;
					resultMap.put("result",result);
					resultMap.put("msg",msg);
					return resultMap;
				}//try
				
			} else if(vo.get(i).get("sign").equals("D")){
				
				param.put("employeeCode",vo.get(i).get("employeeCode"));
				param.put("bonusCode",vo.get(i).get("bonusCode"));
				param.put("bonusDate",vo.get(i).get("bonusDate"));
				param.put("bonusSalary",vo.get(i).get("bonusSalary"));
				try {
					dao.delEmployeeBonusInfo(param);
				} catch (Exception e) {
					msg="처리실패 : Delete 예외 발생";
					result=false;
					resultMap.put("result",result);
					resultMap.put("msg",msg);
					return resultMap;
				}//try
			}//if
		}//for
		msg="처리성공 : 정상적으로 처리 되었습니다.";
		
		result=true;
		resultMap.put("result",result);
		resultMap.put("msg",msg);
		log.debug("==========setEmployeeBonusInfoList:종료==========");
		log.debug(resultMap);
		
		return resultMap;
	}
}
